package com.jiyongjun.bms.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;

/**
 * description:
 *
 * @author yongjun.ji
 * @date 2018/9/5 14:22
 */
public class FileUtil {
    /**
     * 计算文件的 MD5 值
     * <p>Apache common codec 库也可以计算文件MD5或sha1</p>
     * @param file
     * @return
     */
    public static String getFileMD5(File file) {
        if (!file.isFile()) throw new IllegalArgumentException("argument must be a file");
        MessageDigest digest = null;
        byte[] buffer = new byte[8192];
        int len;
        try (FileInputStream in = new FileInputStream(file);) {
            digest = MessageDigest.getInstance("MD5");
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            return bigInt.toString(16);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 计算文件的 SHA-1 值
     *
     * @param file
     * @return
     */
    public static String getFileSha1(File file) {
        if (!file.isFile()) throw new IllegalArgumentException("argument must be a file");
        MessageDigest digest = null;
        byte[] buffer = new byte[8192];
        int len;
        try (FileInputStream in = new FileInputStream(file)) {
            digest = MessageDigest.getInstance("SHA-1");
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            return bigInt.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("E:\\安裝包\\ideaIU-2018.1.5 - 副本.exe");
        File file = path.toFile();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("hello".getBytes());
        buffer.flip();
        FileChannel fileChannel = (FileChannel) Files.newByteChannel(path, StandardOpenOption.READ, StandardOpenOption.WRITE);
        fileChannel = fileChannel.position(file.length() - 100000);
        int len = 0;
        while ((len = fileChannel.write(buffer)) > 0) {
            System.out.println(fileChannel.position());
            System.out.println(new String(buffer.array(), buffer.position(), len));
        }
        File file1 = new File("E:\\安裝包\\ideaIU-2018.1.5.exe");
        String fileMD5 = FileUtil.getFileMD5(file);
        String fileMD51 = FileUtil.getFileMD5(file1);
        System.out.println(fileMD5 + " : " + fileMD51);
        System.out.println("是否相同：" + fileMD5.equals(fileMD51));
    }
}
